package com.pozu.new_retail.controllers;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pozu.new_retail.dao.base.JsonResult;
import com.pozu.new_retail.service.MessageService;
import com.pozu.new_retail.utils.CodeConstants;
import com.pozu.new_retail.utils.CodeProperties;

@Controller
public class MessageController {

	@Autowired
	private MessageService messageService;
	
	/***
	 * 查看各种类型的消息
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/api/messages/list", method = RequestMethod.GET)
	public Object get(HttpServletRequest request) throws Exception{
		
		int appId = Integer.parseInt(request.getHeader("appId"));//那个小程序
		
		Long userId = Long.parseLong(request.getHeader("userId"));
		
		int type = Integer.parseInt(request.getParameter("type"));//消息类型0点赞帖子1评论帖子2帖子被精选3物流信息
		
		int pageStart = Integer.parseInt(request.getParameter("pageStart"));//第几页从0开始
		int pageSize = Integer.parseInt(request.getParameter("pageSize"));//一页显示几条数据
		
		HashMap<String, Object> map = new HashMap<String, Object>();
		
		map.put("pageStart", pageStart*pageSize);
		map.put("pageSize", pageSize);
		map.put("userId", userId);
		map.put("type", type);
		map.put("appId", appId);

		JsonResult jr = new JsonResult();

		jr.respCode = CodeConstants.SUCCESS_CODE;
		jr.respMsg = CodeProperties.getString(CodeConstants.SUCCESS_MSG);
		
		if (type == 0 ) {
			jr.respData = messageService.queryUpvoteMessageByMap(map);
		}else if (type == 1 ) {
			jr.respData = messageService.queryCommentMessageByMap(map);
		}else if (type == 2 ) {
			jr.respData = messageService.queryUgcMessageByMap(map);
		}else if (type == 3 ) {
			jr.respData = messageService.queryOrderMessageByMap(map);
		}
		
		messageService.updateMessage(map);

		return jr;
	}
	
	/***
	 * 统计各类消息数量
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/api/messages", method = RequestMethod.GET)
	public Object getMessage(HttpServletRequest request) throws Exception{
		
		int appId = Integer.parseInt(request.getHeader("appId"));//那个小程序
		
		Long userId = Long.parseLong(request.getHeader("userId"));

		JsonResult jr = new JsonResult();

		jr.respCode = CodeConstants.SUCCESS_CODE;
		jr.respMsg = CodeProperties.getString(CodeConstants.SUCCESS_MSG);
		jr.respData = messageService.queryMessageInfo(userId,appId);

		return jr;
	}
	
}
